package com.one.group.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.one.group.admin.mapper.CollectMaterialsMapper;
import com.one.group.model.dto.CollectMaterialsInsertDto;
import com.one.group.model.dto.CollectMaterialsQueryDto;
import com.one.group.model.dto.InventorySummaryUpdateDto;
import com.one.group.model.entity.CollectMaterials;
import com.one.group.model.entity.InventoryFlowDetail;
import com.one.group.model.entity.MaterialPrice;
import com.one.group.model.vo.CollectMaterialsListVo;
import com.one.group.service.CollectMaterialsService;
import com.one.group.service.InventorySummaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class CollectMaterialsServiceImpl extends ServiceImpl<CollectMaterialsMapper, CollectMaterials> implements CollectMaterialsService {
    @Autowired
    InventorySummaryService inventorySummaryService;
    @Override
    public List<CollectMaterialsListVo> findCollectMaterialsByQueryDto(CollectMaterialsQueryDto collectMaterialsQueryDto) {
        return this.getMapper().findCollectMaterialsByQueryDto(collectMaterialsQueryDto);
    }

    @Override
    @Transactional
    public Boolean insertCollectMaterials(CollectMaterialsInsertDto collectMaterialsInsertDto) {
        CollectMaterialsQueryDto collectMaterialsQueryDto = new CollectMaterialsQueryDto();
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id"));
        collectMaterialsQueryDto.setTenantId(tenantId);
        collectMaterialsQueryDto.setRepairReportsId(collectMaterialsInsertDto.getRepairReportsId());
        List<CollectMaterialsListVo> collectMaterialsByQueryDtoList = findCollectMaterialsByQueryDto(collectMaterialsQueryDto);
        List<MaterialPrice> materialPriceList = collectMaterialsInsertDto.getMaterialPriceList();
        if (collectMaterialsByQueryDtoList.size()==0){
            for (MaterialPrice materialPrice : materialPriceList) {
                CollectMaterials collectMaterials = new CollectMaterials();
                collectMaterials.setQuantity(materialPrice.getQuantity());
                collectMaterials.setQuantityIssued(materialPrice.getQuantity());
                collectMaterials.setRepairReportsId(collectMaterialsInsertDto.getRepairReportsId());
                collectMaterials.setMaterialId(materialPrice.getMaterialId());
                collectMaterials.setPrice(materialPrice.getPrice());
                this.getMapper().insertSelective(collectMaterials);
                materialPrice.setPrice(materialPrice.getPrice().multiply(BigDecimal.valueOf(2)).divide(BigDecimal.valueOf(3)));
                materialPrice.setQuantity(-materialPrice.getQuantity());
            }
            InventorySummaryUpdateDto inventorySummaryUpdateDto = new InventorySummaryUpdateDto();
            inventorySummaryUpdateDto.setMaterialPriceList(materialPriceList);
            inventorySummaryUpdateDto.setRepositoryId(collectMaterialsInsertDto.getRepositoryId());
            inventorySummaryService.updateInventorySummaryByUpdateDto(inventorySummaryUpdateDto);
        }
        else {
            for (MaterialPrice materialPrice2 : materialPriceList){
                CollectMaterials collectMaterials = new CollectMaterials();
                collectMaterials.setMaterialId(materialPrice2.getMaterialId());
                collectMaterials.setPrice(materialPrice2.getPrice());
                collectMaterials.setRepairReportsId(collectMaterialsInsertDto.getRepairReportsId());
                for (CollectMaterialsListVo collectMaterialsListVo:collectMaterialsByQueryDtoList){
                    // 推荐：使用 compareTo 进行 BigDecimal 比较
                    if (materialPrice2.getMaterialId().equals(collectMaterialsListVo.getMaterialId()) &&
                            materialPrice2.getPrice().compareTo(collectMaterialsListVo.getPrice()) == 0) {
                        collectMaterials.setId(collectMaterialsListVo.getId());
                        if (collectMaterialsInsertDto.getStatus()==0) {
                            collectMaterials.setQuantityIssued(collectMaterialsListVo.getQuantityIssued()+materialPrice2.getQuantity());
                            collectMaterials.setQuantity(collectMaterials.getQuantityIssued()-collectMaterialsListVo.getQuantityReturned());
                        }
                        else if (collectMaterialsInsertDto.getStatus()==1) {
                            collectMaterials.setQuantityReturned(collectMaterialsListVo.getQuantityReturned()+materialPrice2.getQuantity());
                            collectMaterials.setQuantity(collectMaterialsListVo.getQuantityIssued()-collectMaterials.getQuantityReturned());
                        }
                    }
                }
                if (collectMaterials.getQuantityIssued()==null&&collectMaterials.getQuantityReturned()==null) collectMaterials.setQuantityIssued(materialPrice2.getQuantity());
                this.getMapper().insertOrUpdateSelective(collectMaterials);
                if (collectMaterialsInsertDto.getStatus()==0) {
                    materialPrice2.setPrice(materialPrice2.getPrice().multiply(BigDecimal.valueOf(2)).divide(BigDecimal.valueOf(3)));
                    materialPrice2.setQuantity(-materialPrice2.getQuantity());
                }
                else {
                    materialPrice2.setPrice(materialPrice2.getPrice().multiply(BigDecimal.valueOf(2)).divide(BigDecimal.valueOf(3)));
                }
            }
            InventorySummaryUpdateDto inventorySummaryUpdateDto = new InventorySummaryUpdateDto();
            inventorySummaryUpdateDto.setMaterialPriceList(materialPriceList);
            inventorySummaryUpdateDto.setRepositoryId(collectMaterialsInsertDto.getRepositoryId());
            inventorySummaryService.updateInventorySummaryByUpdateDto(inventorySummaryUpdateDto);
        }
    return true;
    }
}
